import UIKit
enum XRadiusType {
    case xtopLeft  // 上左
    case xtopRight // 上右
    case xbottomLeft // 下左
    case xbottomRight // 下右
    case xtops // 上左 + 上右
    case xbottoms // 下左 + 下右
    case xtopLeftBottomRight // 上左 + 下右
    case xtopRightBottomLeft // 上右 + 下左
    case xtopsBottomRight // 上左 + 上右 + 下右
    case xtopsBottomLeft // 上左 + 上右 + 下左
    case xallCorners // 所有
}

extension UIView {
    @inline(__always)
    func draw(_ radius: CGFloat, _ rect: CGRect, _ cornerType: XRadiusType = .xallCorners) {
        
        var corner: UIRectCorner = .allCorners
        switch cornerType {
            case .xtopLeft:
                corner = .topLeft
            case .xtopRight:
                corner = .topRight
            case .xbottomLeft:
                corner = .bottomLeft
            case .xbottomRight:
                corner = .bottomRight
            case .xtops:
                corner = .topLeft.union(.topRight)
            case .xbottoms:
                corner = .bottomLeft.union(.bottomRight)
            case .xtopLeftBottomRight:
                corner = .topLeft.union(.bottomRight)
            case .xtopRightBottomLeft:
                corner = .topRight.union(.bottomLeft)
            case .xtopsBottomRight:
                corner = .topLeft.union(.topRight).union(.bottomRight)
            case .xtopsBottomLeft:
                corner = .topLeft.union(.topRight).union(.bottomLeft)
            default:
                corner = .allCorners
        }
        let path = UIBezierPath.init(roundedRect: rect, byRoundingCorners: corner, cornerRadii: CGSize(width: radius, height: radius)).cgPath
        let layer = CAShapeLayer.init()
        layer.path = path
        self.layer.mask = layer
    }
}

